package com.rs.game.player.content;

import java.sql.*;

/**
 * AutoDonation made for GeryonRS
 * 
 * @author Swirl <obruza@gmail.com>
 */

import com.rs.game.World;
import com.rs.game.player.Player;
import com.rs.utils.Logger;
import com.rs.utils.Utils;

public class ForumRights {

	public static Connection con = null;
	public static Statement stmt;
	public static ResultSet rs = null;

	public static void createConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://geryonrs.com/treetop_geryonforums", "treetop_geryon", "cxzdsaewq321");
			stmt = con.createStatement();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static ResultSet query(String s) throws SQLException {
		try {
			if (s.toLowerCase().startsWith("select")) {
				ResultSet rs = stmt.executeQuery(s);
				return rs;
			} else {
				stmt.executeUpdate(s);
			}
			return null;
		} catch (Exception e) {
			destroyConnection();
			createConnection();
			e.printStackTrace();
		}
		return null;
	}

	public static void destroyConnection() {
		try {
			stmt.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static boolean setRights(Player player) {// sets forum rights
		try {
			int count = 0;
			createConnection();// creates connection
			rs = stmt.executeQuery("SELECT * from members WHERE members_seo_name = '" + player.getUsername().replaceAll("_", "-").toLowerCase() + "'");
			if (rs.next()) {
				count = rs.getMetaData().getColumnCount();
			}
			
			if (count == 76) {
				double forumRights = rs.getDouble("donate_amount");
				double donated = player.getDonatedAmount();
				double donationAmount = forumRights - donated;
				player.setDonatedAmount(forumRights);
				if (forumRights < 5) {
					player.getPackets().sendGameMessage("<col=FF0000>You haven't donated enough, you must donate at least $5 to become a donator, you have donated: $" + (int) forumRights + ".</col>");
				}
				if (forumRights >= 5 && player.isDonator()) {
					player.getPackets().sendGameMessage("<col=FF0000>You haven't donated enough, you must donate at least $25 to become an extreme donator, you have donated: $" + (int) forumRights + ".</col>");
				}
				if (forumRights >= 25 && player.isDonator()) {
					player.getPackets().sendGameMessage("<col=FF0000>You are at the top of the donator list, you have donated a total of $" + (int) forumRights + ".");
				}
				if (forumRights >= 5 && !player.isDonator()) {
					World.sendWorldMessage("<img=7><col=FF0000>News: " + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just donated and became a donator!</col>", false);
					player.setDonator(true);
				} else if (forumRights >= 25 && !player.isExtremeDonator()) {
					World.sendWorldMessage("<img=7><col=FF0000>News: " + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just donated and became an extreme donator!</col>", false);
					player.setExtremeDonator(true);
				}
				if (player.isExtremeDonator()) {
					player.setSpins((player.getSpins() + (int) donationAmount) * 4);
					player.getPackets().sendGameMessage("You received " + (int) donationAmount + " spins!");
				}
				
			} else {
				player.getPackets().sendGameMessage("<col=FF0000>There was an error with the process, please make sure your forum username matches your in-game name.</col>");
				Logger.log("ForumRights", player.getUsername() + " does not exist.");
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			destroyConnection();
		} catch (Exception e) {
			Logger.log("ForumRights", "Error, could not get information for " + player.getUsername() + ".");
			e.printStackTrace();
			return false;
		}
		return true;
	}

}